package com.imchen.demov4;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.kuaiyouxi.gamepad.sdk.shell.assist.SDKAssist;

import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;

public class RegularAd {

    public static String PREFERENCE_NAME = "regular";
    public static String SHOW_REGULAR = "2,2,3";
    private static int showIndex = 0;
    private static long showAt;
    private static long bootTime = System.currentTimeMillis();
    private static int mixShowTime = 5 * 1000;
    private static int homeTime = 20 * 1000;
    private static String blockList = "gt_log_in,gt_init_info,gt_scene_levup,gt_ad_button_show,gt_ad_button_click,gt_ad_show,gt_ad_show_end,gt_levelup,gt_get_coins,gt_guide";
    private static HashSet<String> blockSet = new HashSet<>();
    public static HashMap<String, Integer> videoTimeMap;
    public static WeakReference<Context> mContext;
    public static boolean isShowAlert = false;

    static {
        String[] blockLists = blockList.split(",");
        Collections.addAll(blockSet, blockLists);
        videoTimeMap = new HashMap<>();
        videoTimeMap.put("2000000", 5);
        videoTimeMap.put("10000000", 10);
        videoTimeMap.put("50000000", 15);
    }

    public static void init(Context context) {
        if (mContext == null) {
            mContext = new WeakReference<Context>(context);
        }
    }

    public static void showAdWithRegular() {
        showAdWithRegular("");
    }

    public static void showAdWithRegular(String key) {
        if (shouldBlock(key)) {
            return;
        }
        if (shouldAvoidHome(homeTime)){
            return;
        }
        String[] showRegulars = SHOW_REGULAR.split(",");
        int adType = Integer.parseInt(showRegulars[showIndex]);
        Log.d("imchen", "Regular index: " + showIndex + " adType: " + adType);
        if (adType == 1) {
//            插屏
            showLogTimeLimit();
        } else if (adType == 2) {
//全屏插屏
            SDKAssist.showFull();
            showIndex += 1;
        } else if (adType == 3) {
//            激励视频
            showIndex += 1;
            SDKAssist.showVideoAd();
        }


        if (showIndex >= showRegulars.length) {
            showIndex = 0;
        }
    }

    public static void showLogTimeLimit() {
        if (System.currentTimeMillis() - showAt >= mixShowTime) {
            showIndex += 1;
            showAt = System.currentTimeMillis();
            SDKAssist.showLog();
        }
    }

    public static boolean shouldAvoidHome(int homeTime){
        return System.currentTimeMillis()-bootTime>=homeTime;
    }

    public static void showLogAvoidHome(){
        if (System.currentTimeMillis()- bootTime >20*1000){
            SDKAssist.showLog();
        }else {
            Log.i("System","System time:"+(System.currentTimeMillis()- bootTime));
        }
    }

    public static boolean shouldBlock(String key) {
        return blockSet.contains(key);
    }

    public static boolean showReward(String key) {
        SDKAssist.showVideoAd();
        int max;
        if (!videoTimeMap.containsKey(key)) {
            max = 5;
        } else {
            max = videoTimeMap.get(key);
        }
        int current = getInt(key, 0);
        current++;
        int remain = max - current;
        if (remain <= 0) {
            putInt(key, 0);
            return true;
        }
        putInt(key, current);
        showAlertMsg((Activity) mContext.get(), "再看" + remain + "次视频解锁");
        return false;
    }

    public static void showAlertMsg(final Activity activity, final String msg) {
        if (!isShowAlert) {
            return;
        }
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                builder.setTitle("提示");
                builder.setMessage(msg);
                builder.create();
                builder.setPositiveButton("确定", null);
                builder.show();
            }
        });

    }

    public static boolean putInt(String key, int value) {
        SharedPreferences settings = mContext.get().getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    public static int getInt(String key, int defaultValue) {
        SharedPreferences settings = mContext.get().getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getInt(key, defaultValue);
    }

}
